package com.wxzz.elearing.course.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wxzz.elearing.course.entity.*;
import com.wxzz.elearing.course.mapper.*;
import com.wxzz.elearing.course.service.ICurriculumSectionService;
import com.wxzz.elearing.course.vo.LastSeeVo;
import com.wxzz.elearning.common.aop.Idempotent;
import com.wxzz.elearning.common.constants.RedisConstants;
import com.wxzz.elearning.common.exception.ServiceException;
import com.wxzz.elearning.common.vo.JgPageVo;
import com.wxzz.elearning.common.vo.ReturnVO;
import jdk.nashorn.internal.runtime.options.Option;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

/**
 * <p>
 * 课程章节 服务实现类
 * </p>
 *
 * @author
 * @since 2021-03-10
 */
@Service
public class CurriculumSectionServiceImpl extends ServiceImpl<CurriculumSectionMapper, CurriculumSection> implements ICurriculumSectionService {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Autowired
    private CurriculumSectionMapper curriculumSectionMapper;

    @Autowired
    private StudyPlanUnitMapper studyPlanUnitMapper;

    @Autowired
    private CurriculumUnitMapper curriculumUnitMapper;

    @Idempotent
    @Override
    public ReturnVO setLastSee(Long wxUserId, Long curriculumSectionId) {
        CurriculumSection curriculumSection = curriculumSectionMapper.selectById(curriculumSectionId);
        Optional.ofNullable(curriculumSection).orElseThrow(() -> new ServiceException("该章节已不存在"));
        //记录上次看到的章节
        redisTemplate.opsForValue().set(RedisConstants.SECTION_LAST_SEE + wxUserId + ":" + curriculumSection.getCurriculumId(), curriculumSection.getSectionName());
        CurriculumUnit curriculumUnit = curriculumUnitMapper.selectById(curriculumSection.getCurriculumUnitId());
        Optional.ofNullable(curriculumUnit).orElseThrow(() -> new ServiceException("该章节已无对应单元"));
        List<LastSeeVo> lastSeeVoOptional = studyPlanUnitMapper.selectUnitByCurriculumId(curriculumSection.getCurriculumId());
        //记录学习计划中的所学单元
        if (lastSeeVoOptional.size()!=0){
            redisTemplate.opsForValue()
                    .set(RedisConstants.PLAN_SECTION_LAST_SEE + wxUserId + ":" + lastSeeVoOptional.get(0).getId(), lastSeeVoOptional.get(0).getUnitName() + "-" + curriculumUnit.getUnitName() + "-" + curriculumSection.getSectionName());
        }
        return new ReturnVO("记录成功", null);
    }

    @Override
    public ReturnVO setReturnSee(Long wxUserId, Long curriculumId, Long seeTime, Integer status) {
        String s = redisTemplate.opsForValue().get(RedisConstants.CURRICULUM_LAST_SEE_TIME + wxUserId + ":" + curriculumId);
        if (StringUtils.isNotBlank(s)) {
            String[] split = s.split(":");
            if (split[1] != null && "1".equals(split[1])) {
                return new ReturnVO("记录成功", null);
            }
        }
        redisTemplate.opsForValue().set(RedisConstants.CURRICULUM_LAST_SEE_TIME + wxUserId + ":" + curriculumId, seeTime + ":" + status);
        return new ReturnVO("记录成功", null);
    }

    @Autowired
    private WxUserCurriculumSectionMapper wxUserCurriculumSectionMapper;

    @Override
    public ReturnVO<List<WxUserCurriculumSection>> listWxUserCurriculumHistory(Long wxUserId, Long curriculumSectionId) {
        List<WxUserCurriculumSection> wxUserCurriculumSections = wxUserCurriculumSectionMapper.selectList(new LambdaQueryWrapper<WxUserCurriculumSection>()
                .eq(WxUserCurriculumSection::getCurriculumSectionId, curriculumSectionId)
                .eq(WxUserCurriculumSection::getWxUserId, wxUserId));
        return  new ReturnVO("列出历史章节测评记录成功", wxUserCurriculumSections);
    }
}
